package com.tyc.springframework.proto;

import com.google.protobuf.InvalidProtocolBufferException;
import com.tyc.protobuf.UserModel;

/**
 * 测试protobuf
 *
 * @author tyc
 * @version 1.0
 * @date 2021-08-25 14:28:16
 */
public class Test {
    public static void main(String[] args) throws InvalidProtocolBufferException {
        byte[] bytes = objToByte(126726178L, "张三", "cnuidjsm7823jnjidsw8u");
        System.out.println(byteToObj(bytes));
    }

    public static byte[] objToByte(Long id,String name,String password){
        UserModel.User commonUser = UserModel.User.newBuilder()
                .setId(id)
                .setName(name)
                .setEmail(password).build();
        byte[] bytes = commonUser.toByteArray();
        return bytes;
    }

    public static Long byteToObj(byte[] bytes) throws InvalidProtocolBufferException {
        UserModel.User o = UserModel.User.parseFrom(bytes);
        return o.getId();
    }
}
